package com.farm.kaiping.controller;


import com.farm.common.security.AnonymousAccess;
import com.farm.kaiping.entity.SysDict;
import com.farm.kaiping.entity.SysDictItem;
import com.farm.kaiping.po.SysDictPO;
import com.farm.kaiping.service.SysDictService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 字典表(SysDict)表控制层
 *
 * @author zhuhui
 * @since 2024-11-26 22:58:45
 */
@RestController
@RequestMapping("/sysDict")
@AllArgsConstructor
@Api(tags = "字典表控制器")
public class SysDictController {

	private final SysDictService sysDictService;

	@ApiOperation(value = "分页查询字典表", notes = "权限：view_sysDict_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDict_page')")
	@PostMapping("/page")
	public R<Page<SysDict>> getSysDictPage(@RequestBody SysDictPO sysDictPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysDictPO);
		return R.ok(sysDictService.getSysDictPage(page, sysDictPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询字典表列表", notes = "权限：view_sysDict_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDict_list')")
	@PostMapping("/list")
	public R<List<SysDict>> getSysDictList(@RequestBody SysDictPO sysDictPO) {
		try{
			return R.ok(sysDictService.getSysDictList(sysDictPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询字典表", notes = "权限 view_sysDict_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDict_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysDictService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条字典表", notes = "权限：view_sysDict_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDict_one')")
	@PostMapping("/one")
	public R<SysDict> getSysDict(@RequestBody SysDictPO sysDictPO) {
		try{
			return R.ok(sysDictService.getSysDict(sysDictPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增字典表", notes = "权限：view_sysDict_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDict_add')")
	@PostMapping("/add")
	public R saveSysDict(@RequestBody SysDictPO sysDictPO) {
		try{
		    return R.ok(sysDictService.saveSysDict(sysDictPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改字典表", notes = "权限：view_sysDict_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDict_edit')")
	@PostMapping("/edit")
	public R updateSysDict(@RequestBody SysDictPO sysDictPO) {
		try{
		    return R.ok(sysDictService.updateSysDict(sysDictPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除字典表", notes = "权限：view_sysDict_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDict_del')")
	@PostMapping("/del")
	public R removeSysDict(@RequestBody SysDictPO sysDictPO) {
		try{
		    return R.ok(sysDictService.removeSysDict(sysDictPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}

	@ApiOperation(value = "根据字典标识查询字典项",notes = "根据字典标识查询字典项")
	@PostMapping("/getItemList")
	@PreAuthorize("@pms.hasPermission('ROLE_1','VIEW_LOGIN')")
	public R<Map<String, List<SysDictItem>>> getDictItemList(@RequestBody List<String> identifyList) {
		return R.ok(sysDictService.getDictItemList(identifyList));
	}

	@ApiOperation(value = "根据字典标识查询字典项(分组)",notes = "根据字典标识查询字典项(分组)")
	@PostMapping("/getItemGroupList")
	@PreAuthorize("@pms.hasPermission('ROLE_1','VIEW_LOGIN')")
	public R<Map<String, List<SysDictItem>>> getDictItemGroupList(@RequestBody List<String> identifyList) {
		return R.ok(sysDictService.getDictItemGroupList(identifyList));
	}

	@ApiOperation(value = "刷新字典缓存",notes = "刷新字典缓存")
	@PostMapping("/refresh")
	@PreAuthorize("@pms.hasPermission('ROLE_ADMIN','VIEW_LOGIN')")
	public R refreshDictCache() {
		return R.ok(sysDictService.refreshDictCache());
	}


}

